var factor = function(n) {
    var p = 1;
    for(var i=2; i<=n; i++) p *=i;
    return p;
}

var hash=[];
for(var i=0; i<=9; i++) {
    hash[i] = factor(i);
}
var n=2;
while(Math.pow(10,n) < hash[9]*n){
    n++;
};
var limit = Math.pow(10,n);
print("limit=" + limit);
var result = 0;
for(var i=10; i<limit; i++) {
    var sum = 0, tmp0=i, tmp1;
    do{
        tmp1 = tmp0%10;
        tmp0 = (tmp0-tmp1)/10;
        sum = sum + hash[tmp1];
    }while(tmp0>0);
    if(sum == i) {
        result += i;
        print(i);
    }
}

print("sum=" + result);
